package com.zjsru.plan2024.normal;

import com.zjsru.leetcode75.level1.tree.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: cookLee
 * @Date: 2024-01-29
 * 二叉树的中序遍历
 */
public class InorderTraversal {

    /**
     * 主
     * \
     * 输入：root = [1,null,2,3]
     * 输出：[1,3,2]
     * \
     * @param args args
     */
    public static void main(String[] args) {
        InorderTraversal inorderTraversal = new InorderTraversal();
        TreeNode root = new TreeNode(1, null, new TreeNode(2, new TreeNode(3), null));
        System.out.println(inorderTraversal.inorderTraversal(root));
    }

    /**
     * inorder遍历
     * 递归遍历
     * @param root 根
     * @return {@link List}<{@link Integer}>
     */
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        this.helper(root, list);
        return list;
    }

    /**
     * 帮手
     *
     * @param root 根
     * @param list 列表
     */
    private void helper(TreeNode root, List<Integer> list) {
        if (root == null) {
            return;
        }
        this.helper(root.left, list);
        list.add(root.val);
        this.helper(root.right, list);
    }

}
